//获取应用实例
const app = getApp()
var template = require('../../utils/tabBarTemplate/tabBarTemplate.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    classifyItems: [],//分类列表
    curNav:  0,
    curIndex:  0,
    prodListarry:[],//商品列表
    imgpath:'',
    cate_id:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    var that = this;
    //tabbar菜单
    template.tabbar("tabBar", 1, this)//0表示第一个tabbar
    if(options.id) {
     
      that.setData({
        curNav: options.id,
       
      })
     
      
      that.getproductList(options.id);
    }
    
    var _key = wx.getStorageSync("token"),
        imgpath = app.globalData.imgsPath,
        _id = that.data.curNav;


    that.setData({
      _token: _key,
      imgpath: imgpath
    })
    
    that.getsortlist();
    that.getproductList(_id);
    
  },
  //获取分类列表
  getsortlist: function () {
    var that = this;
    var _key = that.data._token;
    wx.request({
      url: app.globalData.httpsPath + '/product/cate',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: _key
      },
      //请求后台数据成功  
      success: function (res) {
        //console.info(res);
        var sortlist = res.data.data;
        that.setData({
          classifyItems: sortlist
        })
        // 隐藏加载框
        wx.hideLoading();
      }
    })

  },
  
  //获取产品列表
  getproductList: function (_id) {
    console.info(_id);
    var that = this;
    var _key = that.data._token;
    wx.request({
      url: app.globalData.httpsPath + '/product/list',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: _key,
        cate_id: _id
      },
      //请求后台数据成功  
      success: function (res) {
        //console.info(res);
        var prodlist = res.data.data.data;
        that.setData({
          prodListarry: prodlist
        })
        // 隐藏加载框
        wx.hideLoading();
      }
    })
  },

  //事件处理函数  
  switchRightTab: function (e) {
    console.info(e)
    var that = this,
      id = e.currentTarget.dataset.id,
      index = parseInt(e.currentTarget.dataset.index);

    that.setData({
      curNav: id,
      curIndex: index
    })
    that.getproductList(id);
  },
  // 获取关键字
  keywordInput: function (e) {
    this.setData({
      keyword: e.detail.value
    })

  },

  // 搜索商品
  srcProduct: function () {
    var that = this;
    if (!that.data.keyword) {
      wx.showToast({
        icon: "none",
        title: '请输入关键字进行查询'
      })
      return;
    }
    wx.request({
      url: app.globalData.httpsPath + '/product/search',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        keyword: that.data.keyword,
        token: wx.getStorageSync("token"),
      },
      //请求后台数据成功  
      success: function (res) {
        var prodlist = res.data.data;
        // console.info(res);
        that.setData({
          prodListarry: prodlist
        })
      }
    })
  },
})